import tester.cobertura.Instrument;

/**
 * @author Adam Blackwell & Ryan Cassidy
 */
// to represent a savings account in a bank
@Instrument 
public class Savings extends AAccount {

	public double interest; // The interest rate at which their money grows.

	Savings(int accountNum, int balance, String name, double interest) {
		super(accountNum, balance, name);
		this.interest = interest;
	}

	/*
	 * TEMPLATE: FIELDS: ... accountNum ... -- int ... balance ... -- int ...
	 * name ... -- String ... interest ... -- double
	 * 
	 * METHODS: ... deposit(int amt) ... -- IAccount ... withdraw(int amt) ...
	 * -- IAccount
	 */

	// produce an updated account after depositing the given amount
	@Override
	public IAccount deposit(int amt) {
		return new Savings(this.accountNum, this.balance + amt, this.name,
				this.interest);
	}

	// produce an updated account after withdrawing the given amount
	@Override
	public IAccount withdraw(int amt) {
		if (this.balance >= amt) {
			return new Savings(this.accountNum, this.balance - amt, this.name,
					this.interest);
		} else {
			throw new RuntimeException(
					"Insufficient Funds in the savings account.");
		}
	}

	@Override
	// Checks if this account is a Savings account (It is).
	public boolean isSavings() {
		return true;
	}

	@Override
	// Turns the account into a Checking account.
	public Savings toSavings() {
		return this;
	}

	@Override
	// Checks if two accounts are the same.
	public boolean same(IAccount that) {
		if (that.isSavings()) {
			return that.toSavings().sameSavings(this);
		} else {
			return false;
		}
	}

	// 
	// @Override
	// // Checks if two accounts are the same.
	// public boolean same(IAccount that) {
	// if(that.isSavings()){
	// return that.toSavings().sameSavings(this);
	// } else {
	// return false;
	// }
	// }

	// Compares the fields of two Checking accounts.
	protected boolean sameSavings(Savings that) {
		return this.name.equals(that.name) && this.balance == that.balance
				&& this.accountNum == that.accountNum
				&& this.interest == that.interest;
	}
}
